package cn.yuanqiao.common.util;


import cn.yuanqiao.common.core.domain.BusinessActivity;
import cn.yuanqiao.common.core.domain.entity.SysUser;
import cn.yuanqiao.common.enums.BusinessBehavior;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.system.service.ISysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

@Component
public class BusinessBehaviorUtil {

    @Autowired
    private ISysOperLogService asyncLogService;

    private  static BusinessBehaviorUtil businessBehaviorUtil;

    @PostConstruct
    public void init() {
        businessBehaviorUtil = this;
        businessBehaviorUtil.asyncLogService = asyncLogService;
    }
    public static void add(Long daId, BusinessBehavior behavior){
        BusinessActivity businessActivity = new BusinessActivity();
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        businessActivity.setCode(behavior.ordinal());
        businessActivity.setName(behavior.getMsg());
        businessActivity.setUserName(sysUser.getNickName());
        businessActivity.setActionTime(DateUtils.getNowDate());
        businessActivity.setDept(sysUser.getDept().getDeptName());
        businessActivity.setDaId(daId);
        businessActivity.setUserId(sysUser.getUserId());
        businessActivity.setDeptId(sysUser.getDept().getDeptId());
        businessActivity.setTableName(SecurityUtils.getTenantCode()+"_BUSINESSACTIVITY");
        businessBehaviorUtil.asyncLogService.insertA001Businessactivity(businessActivity);
    }
    public static void addIdentify(Long daId, BusinessBehavior behavior, SysUser sysUser, Date date, String lable){
        BusinessActivity businessActivity = new BusinessActivity();
        businessActivity.setCode(behavior.ordinal());
        businessActivity.setName(behavior.getMsg()+"-"+lable);
        businessActivity.setUserName(sysUser.getNickName());
        businessActivity.setActionTime(date);
        businessActivity.setDept(sysUser.getDept().getDeptName());
        businessActivity.setDaId(daId);
        businessActivity.setUserId(sysUser.getUserId());
        businessActivity.setDeptId(sysUser.getDept().getDeptId());
        businessActivity.setTableName(SecurityUtils.getTenantCode()+"_BUSINESSACTIVITY");
        try {
            businessBehaviorUtil.asyncLogService.insertA001Businessactivity(businessActivity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void add(List<Long> daId, BusinessBehavior behavior){
        daId.stream().forEach(id->{
            try {
                add(id,behavior);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
